using UnityEngine;
using System.Collections;

public class FatController : MonoBehaviour {
	
	public float walkSpeed = 10f;
	//public float runSpeed = 20f;
	public float initialSpeed;
	public float jumpSpeed = 30f;
	public float gravity = 50f;
	
	public bool running = false;
	public bool areYouAFatty = false;

	private CharacterController characterController;

	public GameObject fatGuy;
	public GameObject skinnyGuy;
	
	private Vector3 moveDirection;

	// Use this for initialization
	void Start () {
		characterController = GetComponent <CharacterController>();
	}
	
	// Update is called once per frame
	void Update () {
		
		if (Input.GetKeyUp(KeyCode.LeftControl))
		{
			Debug.Log("throwing up");
			Messenger.Broadcast("Threw up");
			//areYouAFatty = false;
			//gravity = 50f;
		}
		
//		if (Input.GetKeyDown(KeyCode.LeftShift) && areYouAFatty == false)
//		{
//			running = true;
//		}
//
//		if (Input.GetKeyUp(KeyCode.LeftShift))
//		{
//			running = false;
//		}
	
		if (characterController.isGrounded)
		{
			moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
//			if (running)
//			{
//				moveDirection *= runSpeed;
//			}
//			else
//			{
				moveDirection *= walkSpeed;
//			}
			
			if (moveDirection.magnitude > 0)
			{
				animation.CrossFade("FatBasicWalk");
				animation.wrapMode = WrapMode.Loop;
				//Debug.Log ("Moving with basic walk");
			}
			else
			{
				//Debug.Log ("Idle animation");
				animation.CrossFade ("FatBasicIdle");
				animation.wrapMode = WrapMode.Loop;
			}

//			if (Input.GetKeyDown(KeyCode.Space) && areYouAFatty == false)
//			{
//				moveDirection.y = jumpSpeed;
//				initialSpeed = walkSpeed;
//				if (running)
//				{
//					initialSpeed += runSpeed;
//				}
//			}
		}
		else
		{
			moveDirection += new Vector3(Input.GetAxis("Horizontal")/10,0,Input.GetAxis("Vertical")/10);
			moveDirection.x = Mathf.Clamp(moveDirection.x, -initialSpeed, initialSpeed);
			moveDirection.z = Mathf.Clamp(moveDirection.z, -initialSpeed, initialSpeed);
		}

		moveDirection.y -= gravity * Time.deltaTime;
		characterController.Move(moveDirection * Time.deltaTime);
	}
	
//	void OnTriggerEnter(Collider other)
//	{
//		if (other.gameObject.tag == "Food")
//		{
//			running = false;
//			gravity = 200f;
//			areYouAFatty = true;
//			Messenger.Broadcast("Ate food");
//			Destroy (other.gameObject);
//		}
//	}	
}
